package mn.idax.exchange.otc.util;

import mn.idax.exchange.otc.service.ChatRedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Auther: dison
 * @Date: 2018/7/27 09:15
 * @Description:参数工具类
 */
public class ParamUtils {

    /**
     *
     * 功能描述:
     *  1.将Servlet请求参数转为TreeMap
     *  2.TreeMap主要用于接口签名认证
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/7/27 9:40
     */
    public static TreeMap<String,Object> treeMapToServletRequest(HttpServletRequest request){
        Map<String,String[]> paramMap = request.getParameterMap();
        TreeMap<String,Object> paramTreeMap = new TreeMap<String,Object>();
        paramMap.forEach((key,value) ->paramTreeMap.put(key,request.getParameter(key)));
        return paramTreeMap;
    }

    /**
     *
     * 功能描述: 校验签名
     * @param paramTreeMap
     * @return:
     * @auther: dison
     * @date: 2018/7/27 9:46
     */
    public static void verifyRequestSignToTreeMap(TreeMap<String,Object> paramTreeMap,String secretKey){

        // 1.判断是否存在签名关键字
        String signValue = paramTreeMap.get("sign").toString();
        // 2.请求参数是否存在请求时间戳(校验请求时效性)

        // 3.生成MD5加密参数

        // 4.参数比较

    }


    public static String getLanguageByRequest(HttpServletRequest request){
        String language = request.getHeader("lang");
        if (StringUtils.isEmpty(language)){
            language = request.getHeader("Accept-Language");
        }
        return language;
    }
}
